package com.gg.haoqi.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.gg.haoqi.BaseApp;
import com.gg.haoqi.R;
import com.gg.haoqi.adapter.YjsAdapter;
import com.gg.haoqi.bean.YjsEntity;
import com.gg.haoqi.bean.YjsList;
import com.gg.haoqi.ui.ContentActivity_;
import com.gg.haoqi.utils.AppConstant;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;


@EFragment(R.layout.fragment_yan_jiu_suo)
public class YanJiuSuoFragment extends Fragment {

    @ViewById(R.id.yanjiusuo_lv)
    ListView yjsLv;
    ArrayList<YjsList> yjsList;
    YjsAdapter yjsAdapter;

    @AfterViews
    public void init(){

        yjsList = new ArrayList<YjsList>();
        yjsAdapter = new YjsAdapter(yjsList,getActivity());
        yjsLv.setAdapter(yjsAdapter);
        initData();

        yjsLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ContentActivity_.class);
                intent.putExtra("url", yjsList.get(position).getPost().getAppview());
                startActivity(intent);
            }
        });
    }

    public void initData() {
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(AppConstant.YJS_PATH, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                BaseApp.getInstance().showToast("数据加载失败！");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                YjsEntity entity = JSONObject.parseObject(responseString, YjsEntity.class);
                if (entity != null) {
                    yjsList.addAll(entity.getResponse().getFeeds().getList());
                    yjsAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}
